function varargout = FCS_processor_database(varargin)
% FCS_PROCESSOR_DATABASE M-file for FCS_processor_database.fig
%      FCS_PROCESSOR_DATABASE, by itself, creates a new FCS_PROCESSOR_DATABASE or raises the existing
%      singleton*.
%
%      H = FCS_PROCESSOR_DATABASE returns the handle to a new FCS_PROCESSOR_DATABASE or the handle to
%      the existing singleton*.
%
%      FCS_PROCESSOR_DATABASE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FCS_PROCESSOR_DATABASE.M with the given input arguments.
%
%      FCS_PROCESSOR_DATABASE('Property','Value',...) creates a new FCS_PROCESSOR_DATABASE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before FCS_processor_database_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to FCS_processor_database_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help FCS_processor_database

% Last Modified by GUIDE v2.5 11-Sep-2010 17:24:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FCS_processor_database_OpeningFcn, ...
                   'gui_OutputFcn',  @FCS_processor_database_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before FCS_processor_database is made visible.
function FCS_processor_database_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to FCS_processor_database (see VARARGIN)

% Choose default command line output for FCS_processor_database
handles.output = hObject;
%handles.hist_res=[1:10:2^18];
set(handles.popupmenu1,'String',{'2^15','2^12','2^10','2^8','2^6'});
set(handles.popupmenu1,'Value',4);
set(handles.popupmenu2,'String',{'2^18','2^15','2^12','2^10','2^8'});
set(handles.popupmenu2,'Value',5);
set(handles.popupmenu3,'String',{'moving','sgolay'});
set(handles.popupmenu3,'Value',1);

%handles.connection = myblob_open ('msferry', '2pueblos', 'localhost', 'mf_experiments');

handles.connection = database('mf_flow','msferry','2pueblos');

updateStrainDataTable(handles,handles.uitable6);
updateMediaDataTable(handles,handles.uitable5);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes FCS_processor_database wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = FCS_processor_database_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile('*.fcs');
if(filename==0)
else
    filepath=[pathname,filename];
    
    [handles.data,handles.params]=fca_readfcs(filepath);
    handles.params.date=datestr([handles.params.date ' ' handles.params.starttime],31);

    if(strcmp(handles.params.fcstype,'FCS2.0'))
        parToSave=3;
        handles.params.par=handles.params.par(1:parToSave);
        handles.data=handles.data(:,(1:parToSave));
        handles.params.NumOfPar=parToSave;
    end
    
    handles.params.gate(1)=create_gate(handles,handles.data,handles.params);
    handles.params.ratio=[];
    
    if(get(handles.checkbox10,'Value'))
        yfpIndex=4;
        rfpIndex=5;
        yrRatio=log10(handles.data(:,yfpIndex)./handles.data(:,rfpIndex));
        handles.params.ratio.name='YFP/RFP ratio';
        handles.params.ratio.par=[yfpIndex rfpIndex];
        handles.params.ratio.range=[-2 2];
        handles.params.ratio.data=yrRatio;
        handles.params.ratio.log='true';
        yfpcutoff=3000;
        rfpcutoff=4500;
        handles.params.ratio.cgate=handles.data(:,yfpIndex)>yfpcutoff | handles.data(:,rfpIndex)>rfpcutoff;
        tempGate=false(length(handles.params.ratio.cgate),1);
        tempGate(handles.params.gate(1).indices)=1;
        handles.params.ratio.FSCgate=handles.params.ratio.cgate & tempGate;
        
        sensitivity=0.5;
        smoother='moving';  
        smooth_value=7;
        plotFlagDiff=0;
        binsize=2^8;
        data=handles.params.ratio.data(handles.params.ratio.FSCgate);
        xBinRange=handles.params.ratio.range;
        
        handles.params.ratio.mode = compute_mode_diff_ratio(data,...
            binsize,xBinRange,sensitivity,smoother,smooth_value,plotFlagDiff);
       % 'heh'
    end
    
    %removes logrithmic binning information, however since we normally
    %aquire in linear mode, these values are usually zero and can be
    %removed.
    if(get(handles.checkbox3,'Value')==0)
        for i=1:length(handles.params.par)
            par(i)=rmfield(handles.params.par(i),{'decade','log','logzero'});
        end
        handles.params.par=par;
    end
                
    %This setups up the file table   
    fieldValues{1,1}=fieldnames(rmfield(handles.params,{'par', 'gate','ratio'}));
    fieldValues{1,2}=struct2cell(rmfield(handles.params,{'par', 'gate','ratio'}));
    %fieldValues{:,1}
    set(handles.uitable2,'Data',compute_data_cell(fieldValues)');
    
    set(handles.listbox1,'String',{handles.params.par.name});
    set(handles.listbox2,'String',{handles.params.gate.type});
    
    updateDataTable(handles)
    %updateStrainDataTable(handles,handles.uitable6);
    %updateMediaDataTable(handles,handles.uitable5);
    guidata(hObject, handles);
end

%Executes in batch processing mode
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname,FilterIndex]=uigetfile('*.fcs','MultiSelect','on');
%FilterIndex
if(FilterIndex)
    for j=1:length(filename)
        filepath=[pathname,filename{j}];
        [handles.data,handles.params]=fca_readfcs(filepath);
        handles.params.date=datestr([handles.params.date ' ' handles.params.starttime],31);
        
        handles.params.gate(1)=create_gate(handles,handles.data,handles.params);
        
        %removes logrithmic binning information, however since we normally
        %aquire in linear mode, these values are usually zero and can be
        %removed.
        if(get(handles.checkbox3,'Value')==0)
            for i=1:length(handles.params.par)
                par(i)=rmfield(handles.params.par(i),{'decade','log','logzero'});
            end
            handles.params.par=par;
        end
        name=[handles.params.experiment '_' handles.params.specimen '_' handles.params.tube];
        varName=genvarname(name);
        eval([varName '= {handles.params,handles.data}']);
        save([name '.mat'], varName);
        
        %remove previous iterations data
        handles.data=[];
        handles.params=[];
    end
end
    
%function to save the fcs file data to the clipboard
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%parameters.label{2}=['0% w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (wt)'];
    parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (wt)'];
%    parameters.label{2}=['0% w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (500 \muM)'];
 %   parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (500 \muM)'];
 %   parameters.label{2}=['0% w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (5000 \muM)'];
 %    parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (5000 \muM)'];
 %    parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (5000 \muM)'];
 %    parameters.label{2}=['0% w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (0 \muM)'];
if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)
    params.logPlotFlag=0;
    params.logAxisFlag=0;
    
    params.modePlotFlag=get(handles.checkbox1,'Value');

    params.xbins=2^8;
    params.xBinRange=handles.params.ratio.range;
    %params.title=handles.params.filename;
    params.title=parameters.label{2};

    plot_histogram_general(handles.params.ratio.data(handles.params.ratio.FSCgate),params,handles.params.ratio.mode)
%    plot_histogram_general(handles.params.ratio.data(handles.params.ratio.FSCgate),params)
    
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)
    %parameters.plot_fig=figure;
    output_dir='C:\Users\Michael Ferry\projects\project_thesis\Dissertation\Figures\Reduced_size\Figure files\yGAL\raw_files\';
    parameters.plot_fig=figure;
    parameters.plot_axis=get(parameters.plot_fig,'CurrentAxes');
    parameters.label=[];
%    parameters.dataIndex=yfpIndex;
    parameters.label{1}='Log Gal3/Gal80';
    %parameters.label{2}='Gal3 (5000\muM Met)';
    %parameters.label{2}=handles.params.filename;
    %parameters.label{2}=['0% w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (wt)'];
    %parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (wt)'];
    %parameters.label{2}=['0% w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (500 \muM)'];
    %parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (500 \muM)'];
 %   parameters.label{2}=['0% w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (5000 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (5000 \muM)'];
 %    parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (5000 \muM)'];
 %    parameters.label{2}=['0% w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.00001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.0001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.001) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.005) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.01) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.02) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.05) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.1) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.2) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',0.5) ' %w/v Galactose (0 \muM)'];
%     parameters.label{2}=[sprintf('%1.0g',1) ' %w/v Galactose (0 \muM)'];
     parameters.label{2}=[sprintf('%1.0g',2) ' %w/v Galactose (0 \muM)'];
 %   parameters.transPointX=-.19;
 %   parameters.transPointX=-.34;
%    parameters.transPointX=-.45;
    parameters.transPointX=-.50;
    parameters.label{3}=[output_dir datestr(now,30)];
    parameters.logBinFlag=1;
    parameters.logScaleFlag=1;
    parameters.cMap=[0 2];
    parameters.xbins=2^7;
    parameters.xBinRange=handles.params.ratio.range;
    parameters.ybins=2^7;
    parameters.yBinRange=[1 10^5];
    parameters.xplotRange=handles.params.ratio.range;
    parameters.yplotRange=[2 5.2];
    parameters.labelFS=9;
    parameters.axisFS=8;
    parameters.printedXsize=5;
    parameters.printedYsize=4.75;
    parameters.print='false';
    parameters.drawLine='true';
    parameters.yfpCutoff=3000;
    parameters.rfpCutoff=4500;
    
    parameters.interpolate='false';
    parameters.colorbar='false';
    parameters.cbarLocation='SouthOutside';
    
    line_histogram_scatter(parameters,10.^handles.params.ratio.data(handles.params.ratio.FSCgate),handles.data(handles.params.ratio.FSCgate,3));
    
end
% name=[handles.params.experiment '_' handles.params.tube];
% varName=genvarname(name);
% eval([varName '= {handles.params,handles.data}']);
% save([name '.mat'], varName);

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateDataTable(handles)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    logPlotFlag=get(handles.checkbox6,'Value');
    indicesToPlot=get(handles.listbox1,'Value');
    numbins=handles.params.par(indicesToPlot(1)).range;
    
    val = get(handles.popupmenu1,'Value');
    string_list = get(handles.popupmenu1,'String');
    numScaledBins = str2num(string_list{val});
    
    gateIndex=get(handles.listbox2,'Value');
    data1=handles.data(handles.params.gate(gateIndex).indices,indicesToPlot(1));
    
if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)

    logAxisFlag=get(handles.checkbox2,'Value');
    modePlotFlag=get(handles.checkbox1,'Value');

    plot_histogram(logPlotFlag,logAxisFlag,modePlotFlag,indicesToPlot,...
        numbins,numScaledBins,data1,handles.params,handles.params.gate(gateIndex).par(indicesToPlot(1)).mode)
    
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)

    data2=handles.data(handles.params.gate(gateIndex).indices,indicesToPlot(2));     
    plot_scatter(logPlotFlag,indicesToPlot,numbins,numScaledBins,handles.params,data1,data2)
end


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)
    value=get(handles.listbox1,'Value');
    set(handles.listbox1,'Value',value(1));
    
    set(handles.listbox1,'Max',1);
    updateDataTable(handles)
    
    guidata(hObject, handles);
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)
    set(handles.listbox1,'Max',2);
    
    guidata(hObject, handles);
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
parIndex=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

for i=1:length(parIndex)
    handles.params.gate(gateIndex).par(parIndex(i)).mode=...
        compute_mode_wrapper(handles,handles.data(handles.params.gate(gateIndex).indices,parIndex(i)),...
        handles.params.par(parIndex(i)).range,handles.params.par(parIndex(i)).name);
end
   
updateDataTable(handles)
guidata(hObject, handles);

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%query='INSERT INTO flow_file (datetime, experiment
%table='flow_file';

%find the next auto increment value for the flow file table
auto_id_query='SHOW TABLE STATUS LIKE ''flow_file''';
auto_increment_index=11;
results = fetch(handles.connection,auto_id_query);
flow_file_id=results{auto_increment_index};

%obtain strain data from the gui and then insert flow file into database
strainData=get(handles.uitable6,'Data');

fcsDataToInsert=rmfield(handles.params,{'filepath','par','gate','project','specimen','tube'});
fcsDataToInsert.strain=find([strainData{:,3}]==true);

duplicate_query=['SELECT id FROM flow_file WHERE date = ''' fcsDataToInsert.date ''''];
duplicate_id=fetch(handles.connection,duplicate_query);

if(isempty(duplicate_id))
    'new entry'
    insert(handles.connection,'flow_file',fieldnames(fcsDataToInsert),fcsDataToInsert);
    db_insert_media(handles,flow_file_id)
    db_insert_par(handles,flow_file_id)
    db_insert_gate(handles,flow_file_id)
else
    'previous entry exists'
    update(handles.connection,'flow_file',fieldnames(fcsDataToInsert),fcsDataToInsert,['WHERE date = ''' fcsDataToInsert.date '''']);
    %duplicate_id
    user_response=database_entry_selection;
    switch user_response
        case 1
            %'remove'
            %delete previous gate, mean and mode data
            delete_gate=['DELETE FROM gate1d WHERE flow_file = ' num2str(duplicate_id{1}) ';'];
            exec(handles.connection,delete_gate);
            delete_mode=['DELETE FROM mode WHERE flow_file = ' num2str(duplicate_id{1}) ';'];
            exec(handles.connection,delete_mode);
            delete_mean=['DELETE FROM mean WHERE flow_file = ' num2str(duplicate_id{1}) ';'];
            exec(handles.connection,delete_mean);
            %insert new data
            db_insert_gate(handles,duplicate_id)
        case 2
            %'keep'
            %do nothing
        case 3
            %'append'
            %insert new data, do not do anything with old data
            db_insert_gate(handles,duplicate_id)
    end
    delete_media=['DELETE FROM media WHERE flow_file = ' num2str(duplicate_id{1}) ';'];
    exec(handles.connection,delete_media);
    delete_par=['DELETE FROM par WHERE flow_file = ' num2str(duplicate_id{1}) ';'];
    exec(handles.connection,delete_par);
    
    db_insert_media(handles,duplicate_id)
    db_insert_par(handles,duplicate_id)    
end



%process and insert media data into database
function db_insert_media(handles,flow_file_id)
mediaData=get(handles.uitable5,'Data');
unit_query = 'select type from unit_type';
unit_cell = fetch(handles.connection,unit_query);

indices=find(strcmp('',mediaData(:,2))==0);

for i=1:length(indices)
    media.flow_file=flow_file_id;
    media.component=indices(i);
    media.conc=str2num(mediaData{indices(i),2});
    media.units=find(strcmp(mediaData{indices(i),3},unit_cell)==1);
    insert(handles.connection,'media',fieldnames(media),media);
end

function db_insert_par(handles,flow_file_id)
%process par data and insert into database
%[handles.params.par(1:end).flow_file] = deal(flow_file_id);
 for i=1:length(handles.params.par)
    parDataToInsert=handles.params.par(i);
    parDataToInsert.flow_file=flow_file_id;
    parDataToInsert.drange=parDataToInsert.range;
    parDataToInsert=rmfield(parDataToInsert,'range');
    insert(handles.connection,'par',fieldnames(parDataToInsert),parDataToInsert);
 end

function db_insert_gate(handles,flow_file_id)
%Process gate data and enter into database
for i=1:length(handles.params.gate)
    %find the gate index
    auto_id_query='SHOW TABLE STATUS LIKE ''gate1d''';
    auto_increment_index=11;
    results = fetch(handles.connection,auto_id_query);
    gate_file_id=results{auto_increment_index};
    
    gateDataToProcess=handles.params.gate(i);  
    %remove mode used for gate calculations if present
    if(strcmp(gateDataToProcess.type,'% of FSC-H mode'))
        gateDataToProcess.mode=gateDataToProcess.mode(1).value;
    end
    
    gateDataToProcess=rmfield(gateDataToProcess,{'indices','par'});
    gateDataToProcess.flow_file=flow_file_id;
    insert(handles.connection,'gate1d',fieldnames(gateDataToProcess),gateDataToProcess);
    
    db_insert_meanAndMode(handles,handles.params.gate(i),gate_file_id,flow_file_id)
end

function db_insert_meanAndMode(handles,gate,gate_file_id,flow_file_id)
%process mean and mode data for each of the channels in a gate
for j=1:length(gate.par)
    %enter data into mean table
    mean.value=gate.par(j).mean;
    mean.gate=gate_file_id;
    mean.flow_file=flow_file_id;
    mean.parName=handles.params.par(j).name;
    mean.parIndex=j;
    insert(handles.connection,'mean',fieldnames(mean),mean);
    
    %mode specific calculations
    if(isnan (gate.par(j).mode(1).value))
    else
        for k=1:length(gate.par(j).mode)
            modeToProcess=gate.par(j).mode(k);
            modeToProcess.parName=handles.params.par(j).name;
            modeToProcess.parIndex=j;
            modeToProcess.gate=gate_file_id;
            modeToProcess.flow_file=flow_file_id;
            modeToProcess.mode_number=k;
            insert(handles.connection,'mode',fieldnames(modeToProcess),modeToProcess);
        end
    end
end



function gate=create_gate(handles,data,params)
if(get(handles.uipanel8,'SelectedObject')==handles.radiobutton10)    
    gate.type='FSC-H greater less than';
    gate.parName='FSC-H';
    gate.parIndex=find(strcmp({params.par.name},gate.parName)==1);
    
    gate.lower_bound=str2num(get(handles.edit5,'String'));
    gate.upper_bound=str2num(get(handles.edit7,'String'));
    gate.indices=find(data(:,gate.parIndex) > gate.lower_bound & ...
        data(:,gate.parIndex) < gate.upper_bound);
    gate.counts=length(gate.indices);

   
elseif(get(handles.uipanel8,'SelectedObject')==handles.radiobutton11)
    gate.type='% of FSC-H mode';
    gate.parName='FSC-H';
    gate.parIndex=find(strcmp({params.par.name},gate.parName)==1);

    mode = compute_mode_wrapper(handles,data(:,gate.parIndex),...
        params.par(gate.parIndex).range,gate.parName);
    percent = str2double(get(handles.edit6,'String'));
    gate.lower_bound=mode(1).value*(1-percent/100);
    gate.upper_bound=mode(1).value*(1+percent/100);
    gate.indices=find(data(:,gate.parIndex) > gate.lower_bound & ...
        data(:,gate.parIndex) < gate.upper_bound);
    gate.counts=length(gate.indices);
    gate.mode = mode;
    gate.percent = percent;
   
elseif(get(handles.uipanel8,'SelectedObject')==handles.radiobutton12)
    gate.type='No gating';

    gate.indices=1:params.TotalEvents;
    gate.counts=length(gate.indices);

end
for i=1:length(params.par)
    gate.par(i).mean=mean(data(gate.indices,i)); 
    gate.par(i).mode=compute_mode_wrapper(handles,data(gate.indices,i),params.par(i).range,params.par(i).name);
    %gate.par(i).mode.parName=params.par(i).name;
    %gate.par(i).mode.value=NaN;
end


function mode=compute_mode_wrapper(handles,data,numbins,parName)


plotFlagDiff=get(handles.checkbox8,'Value');

%use default mode finding values
if(get(handles.checkbox7,'Value'))
    if(strcmp(handles.params.fcstype,'FCS2.0'))
        %default for FCS2.0 files
        binsize=2^6;
        lowThresh=10^0.2;
        sensitivity=100;
        smoother='moving';
        smooth_value=11;
    elseif(strcmp(handles.params.fcstype,'FCS3.0'))
        %default for FCS3.0 files
        binsize=2^8;
        lowThresh=10^1;
        sensitivity=0.5;
        smoother='moving';
        smooth_value=11;
    end
else
    val = get(handles.popupmenu2,'Value');
    string_list = get(handles.popupmenu2,'String');
    binsize = str2num(string_list{val});

    val2 = get(handles.popupmenu3,'Value');
    string_list2 = get(handles.popupmenu3,'String');
    smoother = string_list2{val2};
    
    sensitivity=str2num(get(handles.edit3,'String'));
    smooth_value=str2num(get(handles.edit4,'String'));
end

%Determines if only -H channels or all channels should have mode data
%calculated
if(get(handles.uipanel6,'SelectedObject')==handles.radiobutton9)
    if(regexp(parName,'-H'))        
        if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)          
            mode = compute_mode_diff(data,lowThresh,binsize,numbins,sensitivity,smoother,smooth_value,plotFlagDiff);
        elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)         
            mode=compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value);
        end
    else
        mode.value=NaN;
    end    
else   
    if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)        
        mode = compute_mode_diff(data,lowThresh,binsize,numbins,sensitivity,smoother,smooth_value,plotFlagDiff);
    elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)       
        mode=compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value);
    end
end

% --- Executes when selected object is changed in uipanel10.
function uipanel10_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel10 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

if(get(handles.checkbox7,'Value'))
    if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)
        set(handles.popupmenu2,'Value',5);
        set(handles.popupmenu3,'Value',1);
        set(handles.edit3,'String','0.5');
        set(handles.edit4,'String','11');
    elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)
        set(handles.popupmenu2,'Value',3);
        set(handles.popupmenu3,'Value',2);
        set(handles.edit3,'String','150');
        set(handles.edit4,'String','11');
    end
end



function dataCell = compute_data_cell(cell_array)
for i=1:length(cell_array)
    for j=1:length(cell_array{i})
        dataCell{i,j}=cell_array{i}{j};
    end
end
% --- Executes when selected object is changed in uipanel11.
function uipanel11_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel11 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
updateDataTable(handles)

function updateDataTable(handles)
if(get(handles.uipanel11,'SelectedObject')==handles.radiobutton15) 
    updateGateDataTable(handles,handles.params,handles.uitable1);
elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton16) 
    updateParDataTable(handles,handles.uitable1);
elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton17)
    updateModeDataTable(handles,handles.params,handles.uitable1);
% elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton18)
%     updateStrainDataTable(handles,handles.params,handles.uitable1);
% elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton19)
%     updateMediaDataTable(handles,handles.params,handles.uitable1);
end
    
function updateParDataTable(handles,table)
channelIndices=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

fieldValues{1,1}=fieldnames(handles.params.par(channelIndices(1)));
fieldValues{2,1}=struct2cell(handles.params.par(channelIndices(1)));

fieldValues{1,1}{end+1}='gate type';
fieldValues{2,1}{end+1}=handles.params.gate(gateIndex).type;
fieldValues{1,1}{end+1}='mean';
fieldValues{2,1}{end+1}=handles.params.gate(gateIndex).par(channelIndices(1)).mean;
constantFieldLength=length(fieldValues{1,1});

channelStruct=handles.params.gate(gateIndex).par(channelIndices(1));
%channelStruct

if(isnan(channelStruct.mode(1).value))  
else
%channelStruct.mode
%if(exist('channelStruct.mode','var'))
    'mode found';
    for i=1:length(channelStruct.mode)
        fieldValues{1,1}{constantFieldLength+i}=['mode ' num2str(i)];
        fieldValues{2,1}{constantFieldLength+i}=channelStruct.mode(i).value;
    end
%else
    'mode not found';
end

if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)    
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)
    if(length(channelIndices)>1)
        
        channelStruct=handles.params.gate(gateIndex).par(channelIndices(2));
        fieldValues{3,1}=struct2cell(handles.params.par(channelIndices(2)));
        fieldValues{3,1}{end+1}=handles.params.gate(gateIndex).type;
        fieldValues{3,1}{end+1}=handles.params.gate(gateIndex).par(channelIndices(2)).mean;

        if(isnan(channelStruct.mode(1).value))
        else
            %'mode found'
            for i=1:length(channelStruct.mode)
                fieldValues{1,1}{constantFieldLength+i}=['mode ' num2str(i)];
                fieldValues{3,1}{constantFieldLength+i}=channelStruct.mode(i).value;
            end
        end
    end
end
set(table,'Data',compute_data_cell(fieldValues)');

function updateGateDataTable(handles,params,table)

%if(find(strcmp('mode',fieldnames(params.gate))==1))
try    
params.gate.mode=params.gate.mode(1).value;
catch
end
%end
fieldValues{1,1}=fieldnames(rmfield(params.gate,{'par','indices'}));
fieldValues{2,1}=struct2cell(rmfield(params.gate,{'par','indices'}));
%compute_data_cell(fieldValues)'
set(table,'Data',compute_data_cell(fieldValues)');

function updateModeDataTable(handles,params,table)
channelIndices=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

fieldValues{1,1}{1}='name';
fieldValues{2,1}{1}=handles.params.par(channelIndices(1)).name;
fieldValues{1,1}{2}='gate type';
fieldValues{2,1}{2}=handles.params.gate(gateIndex).type;
constantFieldLength=length(fieldValues{1,1});
channelStruct=handles.params.gate(gateIndex).par(channelIndices(1));

if(isnan(channelStruct.mode(1).value))
else

    fieldValues{1,1}{constantFieldLength+1}= 'mode';
    modeFieldNames=fieldnames(channelStruct.mode);
    for i=1:length(modeFieldNames)
        fieldValues{1,1}{end+1}=modeFieldNames{i};
    end
    
    for i=1:length(channelStruct.mode)
       fieldValues{i+1,1}{constantFieldLength+1}=['mode ' num2str(i)];
       modeFieldValues=struct2cell(channelStruct.mode(i));
       for j=1:length(modeFieldValues)
           fieldValues{i+1,1}{end+1}=modeFieldValues{j};
       end

    end
end
%fieldValues{1,1};
%columnname={'parameters','mode 1','mode 2'};
%columnformat={'char','char','char'};
%set(table,'ColumnFormat',columnformat,'ColumnName',columnname);
%compute_data_cell(fieldValues)'
set(table,'Data',compute_data_cell(fieldValues)');

function updateStrainDataTable(handles,table)
columnname={'name','genotype','selected'};
columnformat={'char','char','logical'};
columneditable =  [false false true]; 
strain_query='SELECT name, Genotype FROM sc_strain;';

%strain_query
strain_results = fetch(handles.connection,strain_query);
[strain_results{1:end,3}] = deal(false);

%strain_results
set(table,'ColumnFormat',columnformat,'ColumnName',columnname,'ColumnEditable',columneditable,'Data',strain_results);
%table_data=get(table,'Data');
%table_data

function updateMediaDataTable(handles,table)
unit_query = 'select type from unit_type';
unit_cell = fetch(handles.connection,unit_query)';

columnname={'name','concentraion','units'};
columnformat={'char','numeric',unit_cell};
columneditable =  [false true true]; 

media_query ='SELECT name,'''', type FROM media_component, unit_type WHERE type REGEXP ''% w/v'';';
media_results = fetch(handles.connection,media_query);
set(table,'ColumnFormat',columnformat,'ColumnName',columnname,'ColumnEditable',columneditable,'Data',media_results);



    
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox3



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function radiobutton1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function radiobutton2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function pushbutton2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox6



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox7.
function checkbox7_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox7


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox8.
function checkbox8_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox8
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.listbox1,'Max',1);
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% value=get(handles.listbox1,'Value');
% set(handles.listbox1,'Value',value(1));
% 
% set(handles.listbox1,'Max',1);
% updateDataTable(handles)
% 
% guidata(hObject, handles);

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.listbox1,'Max',2);
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% set(handles.listbox1,'Max',2);
% 
% guidata(hObject, handles);


% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when selected cell(s) is changed in uitable6.
function uitable6_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitable6 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)


% % --- Executes on button press in pushbutton6.
% function pushbutton6_Callback(hObject, eventdata, handles)
% % hObject    handle to pushbutton6 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in checkbox10.
function checkbox10_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox10
